<html>
<head><meta charset="utf-8"><title>ptr-&gt;field for addr_of · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/ptr-.3Efield.20for.20addr_of.html">ptr-&gt;field for addr_of</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="233448515"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/ptr-%3Efield%20for%20addr_of/near/233448515" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Soveu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/ptr-.3Efield.20for.20addr_of.html#233448515">(Apr 07 2021 at 08:06)</a>:</h4>
<p>We could make <code>addr_of</code> accept <code>ptr-&gt;field</code> syntax, it just <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=f552a8169ce1b1bb54b072ceaf4c6b49">looks neat</a></p>



<a name="233452666"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/ptr-%3Efield%20for%20addr_of/near/233452666" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> isHavvy <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/ptr-.3Efield.20for.20addr_of.html#233452666">(Apr 07 2021 at 08:45)</a>:</h4>
<p>Is there a benefit to this other than your aesthetics?</p>



<a name="233491070"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/ptr-%3Efield%20for%20addr_of/near/233491070" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/ptr-.3Efield.20for.20addr_of.html#233491070">(Apr 07 2021 at 13:53)</a>:</h4>
<p>that would make it harder to transition if <code>&amp;raw const</code> ever stabilizes</p>



<a name="233541239"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/ptr-%3Efield%20for%20addr_of/near/233541239" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/ptr-.3Efield.20for.20addr_of.html#233541239">(Apr 07 2021 at 18:44)</a>:</h4>
<p>you already can't perfectly transition because the syntax is already incompatible</p>



<a name="233541377"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/ptr-%3Efield%20for%20addr_of/near/233541377" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/ptr-.3Efield.20for.20addr_of.html#233541377">(Apr 07 2021 at 18:45)</a>:</h4>
<p>yeah fair enough</p>



<a name="233574470"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/ptr-%3Efield%20for%20addr_of/near/233574470" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matt1992 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/ptr-.3Efield.20for.20addr_of.html#233574470">(Apr 07 2021 at 22:42)</a>:</h4>
<p>I'd prefer if <code>ptr-&gt;field</code> syntax inhibited dereference coercions(that create intermediate references).</p>
<p>So this would work:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="n">boxed</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Box</span>::<span class="n">new</span><span class="p">((</span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">)));</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="n">ptr</span>: <span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="n">_</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;*</span><span class="n">boxed</span><span class="p">;</span><span class="w"></span>

<span class="n">std</span>::<span class="n">ptr</span>::<span class="n">addr_of</span><span class="o">!</span><span class="p">(</span><span class="n">ptr</span>-&gt;<span class="mi">2</span><span class="p">);</span><span class="w"></span>
</code></pre></div>
<p>But this would not work:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="n">boxed</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Arc</span>::<span class="n">new</span><span class="p">((</span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">)));</span><span class="w"></span>

<span class="n">std</span>::<span class="n">ptr</span>::<span class="n">addr_of</span><span class="o">!</span><span class="p">(</span><span class="n">boxed</span>-&gt;<span class="mi">2</span><span class="p">);</span><span class="w"></span>
</code></pre></div>
<p>because it creates intermediate references to call <code>Deref::deref</code></p>
<p>I was surprised that there can be deref coercions inside <code>addr_of</code>,since the docs describe <code>addr_of</code> like this:</p>
<blockquote>
<p>Create a <code>const</code> raw pointer to a place, without creating an intermediate reference.</p>
</blockquote>



<a name="233616017"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/ptr-%3Efield%20for%20addr_of/near/233616017" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Pointerbender <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/ptr-.3Efield.20for.20addr_of.html#233616017">(Apr 08 2021 at 07:59)</a>:</h4>
<p>How does <code>addr_of</code> cause a deref coercion in those examples?</p>



<a name="233618106"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/ptr-%3Efield%20for%20addr_of/near/233618106" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matt1992 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/ptr-.3Efield.20for.20addr_of.html#233618106">(Apr 08 2021 at 08:21)</a>:</h4>
<p><span class="user-mention" data-user-id="400735">@Pointerbender</span> <br>
Both of the uses of <code>addr_of</code> here cause a deref coercion:<br>
<a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=efdbe58dfc0753723b8adfe9bd903812">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=efdbe58dfc0753723b8adfe9bd903812</a></p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">use</span><span class="w"> </span><span class="n">std</span>::<span class="n">ops</span>::<span class="n">Deref</span><span class="p">;</span><span class="w"></span>

<span class="k">struct</span> <span class="nc">Wrapper</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">(</span><span class="n">T</span><span class="p">);</span><span class="w"></span>

<span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="n">Deref</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">Wrapper</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">type</span> <span class="nc">Target</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">T</span><span class="p">;</span><span class="w"></span>

<span class="w">    </span><span class="k">fn</span> <span class="nf">deref</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kp">&amp;</span><span class="nc">T</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="fm">println!</span><span class="p">(</span><span class="s">"hello from Deref::deref"</span><span class="p">);</span><span class="w"></span>
<span class="w">        </span><span class="o">&amp;</span><span class="bp">self</span><span class="p">.</span><span class="mi">0</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">main</span><span class="p">(){</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">wrapper</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Wrapper</span><span class="p">((</span><span class="mi">0</span><span class="kt">u128</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">));</span><span class="w"></span>
<span class="w">    </span><span class="n">std</span>::<span class="n">ptr</span>::<span class="n">addr_of</span><span class="o">!</span><span class="p">(</span><span class="n">wrapper</span><span class="p">.</span><span class="mi">2</span><span class="p">);</span><span class="w"></span>

<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">ptr</span>: <span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="n">Wrapper</span><span class="o">&lt;</span><span class="n">_</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="n">wrapper</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="k">unsafe</span><span class="p">{</span><span class="w"> </span><span class="n">std</span>::<span class="n">ptr</span>::<span class="n">addr_of</span><span class="o">!</span><span class="p">((</span><span class="o">*</span><span class="n">ptr</span><span class="p">).</span><span class="mi">2</span><span class="p">)</span><span class="w"> </span><span class="p">};</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="233618423"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/ptr-%3Efield%20for%20addr_of/near/233618423" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Pointerbender <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/ptr-.3Efield.20for.20addr_of.html#233618423">(Apr 08 2021 at 08:24)</a>:</h4>
<p>Oh that is very surprising indeed! Is this a bug in the implementation of <code>addr_of</code> or a bug in its documentation?</p>



<a name="233618604"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/ptr-%3Efield%20for%20addr_of/near/233618604" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Soveu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/ptr-.3Efield.20for.20addr_of.html#233618604">(Apr 08 2021 at 08:26)</a>:</h4>
<p>yeah, it needs documentation and possibly some compiler warnings</p>



<a name="233618664"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/ptr-%3Efield%20for%20addr_of/near/233618664" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Pointerbender <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/ptr-.3Efield.20for.20addr_of.html#233618664">(Apr 08 2021 at 08:27)</a>:</h4>
<p>It does nicely warn about unsafe in this line though: <code>unsafe{ std::ptr::addr_of!((*ptr).2) };</code></p>



<a name="233618753"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/ptr-%3Efield%20for%20addr_of/near/233618753" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Pointerbender <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/ptr-.3Efield.20for.20addr_of.html#233618753">(Apr 08 2021 at 08:28)</a>:</h4>
<p>and if you try to call <code>std::ptr::addr_of!(wrapper.2);</code> when there is a live mutable reference, you will also get a compile error</p>



<a name="233620067"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/ptr-%3Efield%20for%20addr_of/near/233620067" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Pointerbender <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/ptr-.3Efield.20for.20addr_of.html#233620067">(Apr 08 2021 at 08:41)</a>:</h4>
<p>I don't know of a way where you could safely call <code>addr_of</code>  where <code>wrapper</code> is unaligned though (that is possible on <code>*ptr</code> if <code>*ptr</code> is unaligned, hence the <code>unsafe</code>), so it still seems sound luckily. I heard of some intentions to allow <code>addr_of</code> to be called on unaligned <code>*ptr</code> in the future, too. These examples that implicitly call <code>Deref::deref</code> seem relevant to that, but right now it's not possible yet.</p>



<a name="233620588"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/ptr-%3Efield%20for%20addr_of/near/233620588" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Soveu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/ptr-.3Efield.20for.20addr_of.html#233620588">(Apr 08 2021 at 08:46)</a>:</h4>
<p>addr_of can go from pointer to pointer, so both the address and the fields can be unaligned</p>



<a name="233621253"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/ptr-%3Efield%20for%20addr_of/near/233621253" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Pointerbender <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/ptr-.3Efield.20for.20addr_of.html#233621253">(Apr 08 2021 at 08:52)</a>:</h4>
<p><span class="user-mention silent" data-user-id="328907">Soveu</span> <a href="#narrow/stream/122651-general/topic/ptr-.3Efield.20for.20addr_of/near/233620588">said</a>:</p>
<blockquote>
<p>addr_of can go from pointer to pointer, so both the address and the fields can be unaligned</p>
</blockquote>
<p>Currently that's not the case yet, there is another interesting Zulip stream about that currently: <a href="#narrow/stream/213817-t-lang/topic/Alignment.20and.20raw.20pointer.20deref/near/232012526">https://rust-lang.zulipchat.com/#narrow/stream/213817-t-lang/topic/Alignment.20and.20raw.20pointer.20deref/near/232012526</a></p>



<a name="233646754"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/ptr-%3Efield%20for%20addr_of/near/233646754" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/ptr-.3Efield.20for.20addr_of.html#233646754">(Apr 08 2021 at 12:53)</a>:</h4>
<p><span class="user-mention" data-user-id="400735">@Pointerbender</span> it has to be aligned right now: <a href="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/raw.20ref.20semantics/near/232644870">https://rust-lang.zulipchat.com/#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/raw.20ref.20semantics/near/232644870</a></p>



<a name="233652559"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/ptr-%3Efield%20for%20addr_of/near/233652559" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Pointerbender <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/ptr-.3Efield.20for.20addr_of.html#233652559">(Apr 08 2021 at 13:33)</a>:</h4>
<p>Ooo I missed that one, thanks! <span aria-label="grinning" class="emoji emoji-1f600" role="img" title="grinning">:grinning:</span></p>



<a name="233705214"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/ptr-%3Efield%20for%20addr_of/near/233705214" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Soveu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/ptr-.3Efield.20for.20addr_of.html#233705214">(Apr 08 2021 at 18:29)</a>:</h4>
<p>So it still works with <code>repr(packed)</code></p>



<a name="233716865"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/ptr-%3Efield%20for%20addr_of/near/233716865" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matt1992 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/ptr-.3Efield.20for.20addr_of.html#233716865">(Apr 08 2021 at 19:47)</a>:</h4>
<p>Seems like that's about the address of the struct, not the field.<br>
So if it's <code>#[repr(packed(2))]</code> the pointer has to be aligned to a multiple of 2 for the struct, but it doesn't have to be aligned for the field you get inside.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>